
function [f,gr] = mr_no_random2(b)

global dat
global dat2
global g
global D
global k

yy=0;
gra=zeros(1,2*k+0+2);

for i = 1:g
    yt = dat(i).yt;
    wt = dat(i).wt;
    xt = dat(i).xt;
   
    Mg = dat2(i).Mg;
    
    [mr junk] = size(xt);   
  
    
    indp_b1=zeros(mr,1); 
    indp_b2=zeros(mr,1);
    gra_g=zeros(mr,2*k+0+2); 
    
        bb=xt*b(1:k+0)+wt*xt(:,1:k)*b(k+0+1:2*k+0)+Mg*b(2*k+0+1)+b(2*k+0+2)*ones(mr,1); 
        
        indp_b1=indp_b1+ 1./(1+exp(-2*bb));
        indp_b2=indp_b2+ 1./(1+exp(2*bb));
    
     
     yy=yy+ ((ones(mr,1)+yt)'/2*log(indp_b1)+(ones(mr,1)-yt)'/2*log(indp_b2));
    
      
        temp4=[xt wt*xt(:,1:k) Mg ones(mr,1)];  % remember to include the constant term.    
            
        gra_g=gra_g+temp4;
        
 
    gra1=2*((ones(mr,1)+yt)/2-indp_b1)'*gra_g;


    
    gra=gra+gra1;
end

    f=-yy;
    
    
    gr = -gra';
    